#include "hal.h"
#include "relay.h"
#include "timers.h"



/**********************************************************************************
 * Descricao:
 *
 * Parametro(s):
 *					-
 * Retorno:
 *					-
 **********************************************************************************/
void relay_init (void)
{
	//controle do rele da bomba dagua
	GPIO_SetDir    (PORT_RELAYS,PORT_RELAYS, 1);
	GPIO_ClearValue(PORT_RELAYS,PORT_RELAYS   );//inicializa tudo em zero
	//controle inversor de frequencia
	GPIO_SetDir    (PORT_RELAYS_INV,RELAYS_INV, 1); //inicializa os pinos de control do inversor
	GPIO_ClearValue(PORT_RELAYS_INV,PORT_RELAYS   );//inicializa tudo em zero
	//controle motor do bloco e do braco
	//GPIO_SetDir    (PORT_MOTORES,RELAYS_MOTORES, 1); //inicializa os pinos de controle do motor blo e braco
	//GPIO_ClearValue(PORT_MOTORES,RELAYS_MOTORES   );//inicializa tudo em zer
	//Controle do pneumatico
	GPIO_SetDir    (PORT_AVANCOBRACO,RELAY_AVANCOBRACO, 1); //inicializa os pinos de controle do pneumatico
	GPIO_SetDir    (PORT_RECUOBRACO,RELAY_RECUOBRACO, 1); //inicializa os pinos de controle do pneumatico

	//Garante que o braco sera recuado na inicializa
        relay_active(PORT_RECUOBRACO,RELAY_RECUOBRACO);
        relay_active(PORT_AVANCOBRACO,RELAY_AVANCOBRACO);        
	
	//while((sensor_pressao>1900)&&(sensor_pressao!=0)){
        RecuoBraco(); //inicializa recuado
        delay_ms(100);
	relay_deactive(PORT_RECUOBRACO,RELAY_RECUOBRACO);
        //}

	//controle da PONTEH
	GPIO_SetDir    (PORT_PONTEH,RELAYS_PONTEH, 1);
	GPIO_ClearValue(PORT_PONTEH,RELAYS_PONTEH   ); //Ter cuidado ao incializar a ponte H

	//ParadaMotorMola();
	DesativaPonteH();

	//while(sensor_pressao!=0);
} 

/**********************************************************************************
 * Descricao:
 *
 * Parametro(s):
 *					-
 * Retorno:
 *					-
 **********************************************************************************/
void relay_active (uint8_t Port,uint32_t relay){
	GPIO_SetValue(Port,relay);
}

/**********************************************************************************
 * Descricao:
 *
 * Parametro(s):
 *					-
 * Retorno:
 *					-
 **********************************************************************************/
void relay_deactive (uint8_t Port,uint32_t relay)
{
	GPIO_ClearValue(Port,relay);
}
/**********************************************************************************
 * Descricao:
 *
 * Parametro(s):
 *					-
 * Retorno:
 *					-
 **********************************************************************************/
void AvancoMotorMola(void)
{
	relay_active(PORT_PONTEH,(RELAY_AVANCO1|RELAY_RECUO2));
	
	relay_deactive(PORT_PONTEH,(RELAY_RECUO1|RELAY_AVANCO2));
}

 /**********************************************************************************
  * Descricao:
  *
  * Parametro(s):
  *					-
  * Retorno:
  *					-
  **********************************************************************************/
void RecuoMotorMola(void)
{
	relay_deactive(PORT_PONTEH,(RELAY_AVANCO1|RELAY_RECUO2));
	
	relay_active(PORT_PONTEH,(RELAY_RECUO1|RELAY_AVANCO2));
}
 /**********************************************************************************
  * Descricao:void ParadaMotorMola(void)
  * Garante que a ponte H nao estará ativada e assim nao sobrecarregando o circuito.
  * Parametro(s):
  *					-
  * Retorno:
  *					-
  **********************************************************************************/
  void DesativaPonteH(void)
  {
  relay_deactive(PORT_PONTEH,(RELAY_AVANCO1));
  relay_deactive(PORT_PONTEH,(RELAY_RECUO1));
  relay_active(PORT_PONTEH,(RELAY_RECUO2|RELAY_AVANCO2));
  }

 /**********************************************************************************
 * Descricao:void AvancoBraco(void)
 * Realiza a ativacao do pneumatico o qual irá realiza o acionamento para o sentido
 * de travamento do bloco junto ao suporte.
 * Parametro(s):
 *					-
 * Retorno:
 *					-
 **********************************************************************************/
  void AvancoBraco(void){
	relay_deactive(PORT_AVANCOBRACO,RELAY_AVANCOBRACO);
	relay_active(PORT_RECUOBRACO,RELAY_RECUOBRACO);
	//relay_deactive(PORT_RECUOBRACO,RELAY_RECUOBRACO);
	delay_ms(10000);
	relay_active(PORT_AVANCOBRACO,RELAY_AVANCOBRACO);
  }

/**********************************************************************************
* Descricao:void RecuoBraco(void)
* Realiza a ativacao do pneumatico de recuo para realizar a liberacao do braco.
* Parametro(s):
*					-
* Retorno:
*					-
**********************************************************************************/
void RecuoBraco(void){
	relay_deactive(PORT_AVANCOBRACO,RELAY_AVANCOBRACO);
    relay_deactive(PORT_RECUOBRACO,RELAY_RECUOBRACO);
}
